{include file="../application/admin/view/header.html" /}
<link href="/static/css/summernote.css" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="/static/css/summernote-bs4.css">
<link rel="stylesheet" type="text/css" href="/static/css/summernote-lite.css">
<link rel="stylesheet" type="text/css" href="/static/css/uploadify.css">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-12">
	<div class="alert kesn_prompt"></div>
</div>
{if condition="$action eq 'edit'"}
<div class="row input-group col-md-4" style="display: none">
    <span class="input-group-addon">ID</span>
    <input type="text" class="form-control" placeholder="" name="policy_id" value="{$policy.id}" />
  </div>
  <div class="row input-group col-md-4">
    <span class="input-group-addon">文件名</span>
    <input type="text" class="form-control" placeholder="" name="policy_name" value="{$policy.policy}" />
  </div>
  <br/>
  <div>
    发布至仓库：
    <select multiple="multiple" class="form-control multiselect multiselect-info" id="to_where">
      {volist name="warehouse_list" id="warehouse"}
      <option value="{$warehouse.id}" {if condition="$warehouse.selected eq '1'"} selected {/if}>{$warehouse.warehouse}</option>
      {/volist}
    </select>
  </div>
  <br/>
  <div id="attachments_div">
    {volist name="policy.attachments" id="attachment"}
    <div class="uploaded_file">{$attachment.name}<span class="delete_attachment fui-cross" data-file_path="{$attachment.path}" style="color: #E74C3C;" ></span></div>
    {/volist}
    <div id="fileQueue"></div>
      <input type="file" name="attachments_file" id="uploadify" multiple="true"/>
      <p>
        <button type="button" class="btn btn-primary btn-xs" id="uploadStart">上传</button>
        <button type="button" class="btn btn-warning btn-xs" id="uploadCancle">取消上传</button>
      </p>
  </div>
  <br />
  <div class="row">
    <form>
      <textarea id="summernote" name="editordata">{$policy.content}</textarea>
    </form>
      <br/>
      <button class="btn btn-info col-md-offset-9" id="save_policy">保存</button>
  </div>


{/if}

{if condition="$action eq 'add'"}
<div class="row input-group col-md-4" style="display: none">
    <span class="input-group-addon">ID</span>
    <input type="text" class="form-control" placeholder="" name="policy_id"/>
  </div>
	<div class="row input-group col-md-4">
	  <span class="input-group-addon">文件名</span>
	  <input type="text" class="form-control" placeholder="" name="policy_name"/>
	</div>
	<br/>
  <div>
    发布至仓库：
    <select multiple="multiple" class="form-control multiselect multiselect-info" id="to_where">
      {volist name="warehouse_list" id="warehouse"}
      <option value="{$warehouse.id}">{$warehouse.warehouse}</option>
      {/volist}
    </select>
  </div>
  <br/>
	<div id="attachments_div">
		
		<div id="fileQueue"></div>
	    <input type="file" name="attachments_file" id="uploadify" multiple="true"/>
			<p>
				<button type="button" class="btn btn-primary btn-xs" id="uploadStart">上传</button>
				<button type="button" class="btn btn-warning btn-xs" id="uploadCancle">取消上传</button>
			</p>
	</div>
	<br />
	<div class="row">
		<form>
		  <textarea id="summernote" name="editordata"></textarea>
		</form>
     	<br/>
  		<button class="btn btn-info col-md-offset-9" id="save_policy">保存</button>
	</div>

{/if}
</div>
</div>


{include file="../application/admin/view/footer.html" /}
<script src="/static/js/summernote.js"></script>
<script type="text/javascript" src="/static/js/summernote-bs4.js"></script>
<script type="text/javascript" src="/static/js/summernote-lite.js"></script>
<script type="text/javascript" src="/static/lang/summernote-zh-CN.js"></script>
<script type="text/javascript" src="/static/js/jquery.uploadify.js"></script>
<script type="text/javascript">
$("select").select2({dropdownCssClass: 'dropdown-inverse'});

$(document).ready(function() {
  

$('#uploadStart').click(function(){
	$('#uploadify').uploadify('upload','*');
});
$('#uploadCancle').click(function(){
	$('#uploadify').uploadify('cancel');
});
   $("#uploadify").uploadify({
    	'buttonText':'添加附件',
    	'swf'     : '/static/swf/uploadify.swf',
        'uploader': 'upload_attachements',
        'fileObjName' : 'attachments_file',
        'cancelImg': '/static/img/uploadify-cancel.png',
        'queueID': 'fileQueue',
        'auto': false,
        'multi': true,
        // 'method' : 'POST', 
        'removeCompleted' : true,
        'onUploadStart' : function(file) {
        },
        'onUploadSuccess' :function(file,data,res){
         	data = JSON.parse(data);
         	if (data.err != '0') {
         		kesn_prompt('fail',data.content);
         	}else{
         		var attachments_html = '';
         		for(var key in data.file){
         			attachments_html += '<div class="uploaded_file">'+data.file[key].file_name+' <span class="delete_attachment fui-cross" data-file_path="'+data.file[key].file_path+'" style="color: #E74C3C;" ></span></div>';
         		}
         		$('#attachments_div').prepend(attachments_html);
         		kesn_prompt('success',data.content);
         	}
         },
          'onUploadError' : function(file, errorCode, errorMsg, errorString) {
            console.log(errorMsg+' '+errorString);
            kesn_prompt('fail','网络出错！');
        }

    });


   $('#summernote').summernote({
    minHeight: 400,
    lang: 'zh-CN',
    });

});

//删除已上传文件
$('#attachments_div').on('click','.delete_attachment',function(){
	var _this = $(this);
	var policy_id = $('input[name=policy_id]').val();
	var attachments_path = _this.attr('data-file_path');
	 $.ajax({
      url : 'delete_attachment',
      async:true,
      type:'POST',
      data:{
      	policy_id:policy_id,
      	attachments_path:attachments_path
      },
      dataType:'JSON',
      success:function(data,textStatus,jqXHR){
            if (data.err != 0) {
              kesn_prompt('fail',data.content);
            }else if (data.err == 0) {
            	_this.parents('.uploaded_file').remove();
              console.log(_this.parents('.uploaded_file'));
                kesn_prompt('success',data.content);

            }

          },
          error:function(){
            kesn_prompt('fail','网络出错');
          }
    });
});

//保存
$('#save_policy').click(function(){
	var _this = $(this);
	var policy_name = $('input[name=policy_name]').val();
	if (policy_name == '' || policy_name == null) {
		kesn_prompt('fail','文件名不能为空！');
		return;
	}
	var policy_id = $('input[name=policy_id]').val();
	var policy_content = $('textarea#summernote').val();
    var policy_to_where = $('select').val();
    var policy_attachments = new Array();
    $('#attachments_div').find('.uploaded_file').each(function(){
    	var this_file = $(this);
    	var attachment = {};
    	attachment['name'] = this_file.text();
    	attachment['path'] = this_file.find('.delete_attachment').attr('data-file_path');
    	policy_attachments.push(attachment);
    });
    // policy_attachments = JSON.stringify(policy_attachments);
     $.ajax({
      url : 'save_policy',
      async:true,
      type:'POST',
      data:{
      	policy_id:policy_id,
      	policy_name:policy_name,
      	policy_content:policy_content,
      	policy_to_where:policy_to_where,
      	policy_attachments:policy_attachments
      },
      dataType:'JSON',
      success:function(data,textStatus,jqXHR){
            if (data.err != 0) {
              kesn_prompt('fail',data.content);
            }else if (data.err == 0) {
        		if (policy_id == '' || policy_id == null) {
        			_this.removeClass('btn-info');
        			_this.attr('disabled','disabled');
        		}
                kesn_prompt('success',data.content);

            }

          },
          error:function(){
            kesn_prompt('fail','网络出错');
          }
    });

});




</script>

</footer>
</body>
</html>